package com.syh.jn.scm.domain.downstream.entity.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.supplier.repository.finance.PaymentBillRepository;
import com.syh.jn.scm.domain.supplier.repository.finance.ReceiptBillRepository;
import com.syh.jn.scm.dto.downstream.base.DownBankAccountMto;
import com.syh.jn.scm.dto.downstream.base.DownBankAccountSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.UUID;

/**
 * 银行账户管理
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class DownBankAccount extends AbstractSimpleAggregate<Long> {
  /**
   * 新建银行账户
   * @param dto
   * @return
   */
  public static DownBankAccount createBusiness(DownBankAccountSto dto) {
    ExtAssert.notNull(dto, "对象为空");
    ValidationUtils.validate(dto);
    DownBankAccount entity = DownBankAccount.systemCreate(dto);
    entity.setUuid(UUID.randomUUID().toString());
    return entity;
  }
  /**
   * 修改经营主体
   * @param mto
   */
  public void modify(DownBankAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    ExtBeanUtils.copyProperties(mto, this);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }
  /**
   * 启用
   */
  public void enabled() {
    this.status = Short.valueOf("1");
  }
  /**
   * 停用
   */
  public void disabled() {
    this.status = Short.valueOf("-1");
  }

  /**
   * 数据销毁
   */
  public void destroy() {
    PaymentBillRepository paymentBill = (PaymentBillRepository) SpringContextUtils.getBean(PaymentBillRepository.class);
    Wrapper payWrapper = Wrapper.build();
    payWrapper.append(
            Restrictions.or(
                    Restrictions.eq("ourBankInfoId", this.id),
                    Restrictions.eq("otherPartyBankInfoId", this.id)
            )
    );
    Integer payCount = paymentBill.getCount(payWrapper);
    if (payCount > 0) {
      throw new BusinessException("该数据在付款单存在信息,删除失败");
    }
    ReceiptBillRepository receiptBill = (ReceiptBillRepository) SpringContextUtils.getBean(ReceiptBillRepository.class);
    Wrapper receiptWrapper = Wrapper.build();
    receiptWrapper.append(
            Restrictions.or(
                    Restrictions.eq("ourBankInfoId", this.id),
                    Restrictions.eq("otherPartyBankInfoId", this.id)
            )
    );
    Integer receiptCount = receiptBill.getCount(receiptWrapper);
    if (receiptCount > 0) {
      throw new BusinessException("该数据在收款单存在信息,删除失败");
    }
  }
  /**
   * 系统默认的字段值
   * @return
   */
  private static DownBankAccount systemCreate(DownBankAccountSto dto) {
    DownBankAccount entity = new DownBankAccount();
    ExtBeanUtils.copyProperties(dto, entity);
    entity.createTimeStamp = System.currentTimeMillis();
    entity.status = Short.valueOf("1");
    return entity;
  }
  /**
   * 开户公司
   */
  private String accountCompany;
  /**
   * 开户行
   */
  private String accountBank;
  /**
   * 开户支行
   */
  private String accountBranchBank;
  /**
   * 归属id
   */
  private Long businessEntityId;
  /**
   * 支行地址
   */
  private String branchBankAddress;
  /**
   * 开户账号
   */
  private String accountBankCode;
  /**
   * 状态: -1停用, 1可用
   */
  private Short status;
  /**
   * 省市区
   */
  private String area;
}
